package CETranslator

import (
	"ZxDict/pkg/util"
	"sort"
	"strings"
	"unicode"
)

type CETranslator struct{}

func (t CETranslator) Translate(s string) []*CENode {
	if len(s) == 0 {
		return nil
	}

	var ret []*CENode

	for idx, c := range s {
		if unicode.Is(unicode.Han, c) {
			// 第一个汉字直接从字典中获取
			if idx < 1 {
				matchList, isOK := hanziNodeMap[string(c)]
				if !isOK {
					continue
				}
				ret = append(ret, matchList...)
			} else { // 后面的字符从includeCharacterList中进行过滤
				var newList []*CENode
				for _, t := range ret {
					if strings.ContainsRune(t.SimplifiedChinese, c) {
						newList = append(newList, t)
					}
				}
				ret = newList
			}
		}
	}

	sort.Slice(ret, func(i, j int) bool {
		return util.MinDistance(s, ret[i].SimplifiedChinese) < util.MinDistance(s, ret[j].SimplifiedChinese)
	})

	return ret
}
